package com.common.base.widget

import android.content.Context
import android.util.AttributeSet
import android.view.MotionEvent
import androidx.core.view.NestedScrollingChildHelper
import androidx.recyclerview.widget.RecyclerView
import androidx.core.view.ViewCompat

class HorizontalRecyclerView : RecyclerView {
    var rightDragToPage: Boolean = false  //控制滑到最右端,viewpager进行翻页

    private var mNestedScrollingChildHelper: NestedScrollingChildHelper? = null

    constructor(context: Context, rightDragToPage: Boolean) : super(context) {
        this.rightDragToPage = rightDragToPage
    }

    constructor(context: Context) : super(context) {}

    constructor(context: Context, attrs: AttributeSet) : super(context, attrs) {
        mNestedScrollingChildHelper = NestedScrollingChildHelper(this)
        isNestedScrollingEnabled = true
    }


    override fun setNestedScrollingEnabled(enabled: Boolean) {
        if (mNestedScrollingChildHelper != null) {
            mNestedScrollingChildHelper!!.isNestedScrollingEnabled = enabled
        }
    }

    override fun isNestedScrollingEnabled(): Boolean {
        if (mNestedScrollingChildHelper != null) {
            return mNestedScrollingChildHelper!!.isNestedScrollingEnabled
        }
        return false
    }

    override fun startNestedScroll(axes: Int): Boolean {
        if (mNestedScrollingChildHelper != null) {
            return mNestedScrollingChildHelper!!.startNestedScroll(axes)
        }
        return false
    }

    override fun stopNestedScroll() {
        if (mNestedScrollingChildHelper != null) {
            mNestedScrollingChildHelper!!.stopNestedScroll()
        }
    }

    override fun hasNestedScrollingParent(): Boolean {
        if (mNestedScrollingChildHelper != null) {
            return mNestedScrollingChildHelper!!.hasNestedScrollingParent()
        }
        return false
    }

    override fun dispatchNestedPreScroll(dx: Int, dy: Int, consumed: IntArray?, offsetInWindow: IntArray?): Boolean {
        if (mNestedScrollingChildHelper != null) {
            return mNestedScrollingChildHelper!!.dispatchNestedPreScroll(dx, dy, consumed, offsetInWindow)
        }
        return false
    }

    override fun dispatchNestedScroll(dxConsumed: Int, dyConsumed: Int, dxUnconsumed: Int,
                                      dyUnconsumed: Int, offsetInWindow: IntArray?): Boolean {
        if (mNestedScrollingChildHelper != null) {
            return mNestedScrollingChildHelper!!.dispatchNestedScroll(dxConsumed, dyConsumed,
                    dxUnconsumed, dyUnconsumed, offsetInWindow)
        }
        return false
    }

    override fun dispatchNestedFling(velocityX: Float, velocityY: Float, consumed: Boolean): Boolean {
        if (mNestedScrollingChildHelper != null) {
            return mNestedScrollingChildHelper!!.dispatchNestedFling(velocityX, velocityY, consumed)
        }
        return false
    }

    override fun dispatchNestedPreFling(velocityX: Float, velocityY: Float): Boolean {
        if (mNestedScrollingChildHelper != null) {
            return mNestedScrollingChildHelper!!.dispatchNestedPreFling(velocityX, velocityY)
        }
        return false
    }


}

